package org.ofbiz.sys.utils;

import java.sql.Date;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.ofbiz.base.util.UtilValidate;

import javolution.util.FastList;
import javolution.util.FastMap;

public class JsonObjectUtils {

    private static Map<String, Object> removeStamp(Map<String, Object> oldMap) {
	Map<String, Object> newMap = new HashMap<String, Object>();
	// oldMap.remove("createdStamp");
	// oldMap.remove("lastUpdatedStamp");
	if (UtilValidate.isNotEmpty(oldMap)) {
	    newMap.putAll(oldMap);
	    newMap.remove("createdTxStamp");
	    newMap.remove("lastUpdatedTxStamp");
	}
	return newMap;
    }

    public static Map<String, Object> optimizingResults(Map<String, Object> serviceResults) {
	Map<String, Object> newResults = FastMap.newInstance();
	Map<String, Object> removeStampServiceResults = removeStamp(serviceResults);
	for (Map.Entry<String, Object> entry : removeStampServiceResults.entrySet()) {
	    String key = entry.getKey();
	    newResults.put(key, subordResults(serviceResults.get(key)));
	}
	return newResults;
    }

    @SuppressWarnings("unchecked")
    private static Object subordResults(Object o) {
	Object newValue = o;
	if (UtilValidate.isNotEmpty(o)) {
	    if (o instanceof Map) {
		Map<String, Object> map2 = (Map<String, Object>) o;
		Map<String, Object> newMap1 = FastMap.newInstance();
		Map<String, Object> removeStampMap2 = removeStamp(map2);
		for (Map.Entry<String, Object> entry2 : removeStampMap2.entrySet()) {
		    String key2 = entry2.getKey();
		    newMap1.put(key2, subordResults(map2.get(key2)));
		}
		newValue = newMap1;
	    } else if (o instanceof Timestamp) {
		DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		newValue = sdf.format(o);
	    } else if (o instanceof Date) {
		DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // HH:mm:ss
		newValue = sdf.format(o);
	    } else if (o instanceof List) {
		List<Object> list2 = (List<Object>) o;
		Object o2 = list2.get(0);
		if (o2 instanceof Map) {
		    List<Object> newList1 = FastList.newInstance();
		    for (Object o3 : list2) {
			Map<String, Object> map2 = (Map<String, Object>) o3;
			Map<String, Object> newMap1 = FastMap.newInstance();

			Map<String, Object> removeStampMap2 = removeStamp(map2);
			for (Map.Entry<String, Object> entry2 : removeStampMap2.entrySet()) {
			    String key2 = entry2.getKey();
			    newMap1.put(key2, subordResults(map2.get(key2)));
			}
			newList1.add(newMap1);
		    }
		    newValue = newList1;
		} else {
		    return newValue;
		}
	    } else {
		return newValue;
	    }
	}
	return newValue;
    }

    public static Map<String, Object> mapInHandle(Map<String, Object> serviceResults) {
	Map<String, Object> newResults = FastMap.newInstance();
	for (Map.Entry<String, Object> entry : serviceResults.entrySet()) {
	    String key = entry.getKey();
	    if (UtilValidate.isNotEmpty(key)) {
		Object newValue = serviceResults.get(key);
		if (UtilValidate.isNotEmpty(newValue)) {
		    if (!(newValue instanceof Map) && !(newValue instanceof List) && !(newValue instanceof Date)
			    && !(newValue instanceof Timestamp)) {
			if (String.valueOf(newValue).indexOf(",") > -1 && !key.toUpperCase().contains("Id")
				&& !key.equals("id") && !key.equals("ids")) {
			    Object value2 = String.valueOf(newValue);
			    if (isNumeric(String.valueOf(value2))) {
				newResults.put(key, value2);
			    } else {
				newResults.put(key, serviceResults.get(key));
			    }
			} else {
			    newResults.put(key, serviceResults.get(key));
			}
		    } else {
			newResults.put(key, serviceResults.get(key));
		    }
		} else {
		    newResults.put(key, serviceResults.get(key));
		}
	    }
	}
	return newResults;
    }

    public static boolean isNumeric(String str) {
	try {
	    Float.parseFloat(str);
	    return true;
	} catch (NumberFormatException e) {
	    // System.out.println("异常：\"" + str + "\"不是数字...");
	    return false;
	}
    }

    /**
     * 获取上一个月
     * 
     * @return
     */
    public static String getLastMonth() {
	Calendar cal = Calendar.getInstance();
	cal.add(Calendar.MONTH, -1);
	SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	String lastMonth = dft.format(cal.getTime());
	return lastMonth;
    }

    public static void main(String[] args) {
	System.out.println("createdDate".toUpperCase().endsWith("DATE"));
    }

}
